package com.windy.spring.rest;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.windy.spring.dao.DaoPositionHistory;
import com.windy.spring.data.PositionHistory;

/**
 * Handles requests for the application home page.
 */
@Controller
@Transactional
public class WindersMainController {
	
	@Autowired
    private DaoPositionHistory dph;
	
	private static final Logger logger = LoggerFactory.getLogger(WindersMainController.class);
	
	@RequestMapping(value = "/getDataForNearestPositionGBuser", method = RequestMethod.GET)
	@ResponseBody
	public List<PositionHistory> getDataForNearestPositionGBuser(
			@RequestParam (value="id", required=true) Integer id,
			@RequestParam (value="pageNumber", defaultValue="0") Integer pageNumber,
			@RequestParam (value="rowsPerPage", defaultValue="10") Integer rowsPerPage,
			@RequestParam (value="usernameFilter", required=false) String usernameFilter,
			@RequestParam (value="currentLat", required=false) Double currentLat,
			@RequestParam (value="currentLon", required=false) Double currentLon
			) {
		logger.debug(
				"id:" + id + " - " +
				"pageNumber:" + pageNumber + " - " +
				"rowsPerPage:" + rowsPerPage + " - " +
				"usernameFilter:" + usernameFilter + " - " +
				"currentLat:" + currentLat + " - " +
				"currentLon:" + currentLon
				);
		List<PositionHistory> resultlist=(List<PositionHistory>) dph.findByNearestPositionGBuser(id, pageNumber, rowsPerPage, usernameFilter, currentLat, currentLon);
		return resultlist;
	}
	
	@RequestMapping(value = "/findByFavUserGBuser", method = RequestMethod.GET)
	@ResponseBody
	public List<PositionHistory> findByFavUserGBuser(
			@RequestParam (value="favoriteUserId", required=true) Integer favoriteUserId,
			@RequestParam (value="pageNumber", defaultValue="0") Integer pageNumber,
			@RequestParam (value="rowsPerPage", defaultValue="10") Integer rowsPerPage,
			@RequestParam (value="usernameFilter", required=false) String usernameFilter
			) {
		logger.debug(
				"favoriteUserId:" + favoriteUserId + " - " +
				"pageNumber:" + pageNumber + " - " +
				"rowsPerPage:" + rowsPerPage + " - " +
				"usernameFilter:" + usernameFilter);
		List<PositionHistory> resultlist=(List<PositionHistory>) dph.findByFavUserGBuser(favoriteUserId, pageNumber, rowsPerPage, usernameFilter);
		return resultlist;
	}
	
	//test
	@RequestMapping(value = "/getDataForNearestPositionGBuser/{id}/", method = RequestMethod.GET)
	@ResponseBody
	public List<PositionHistory> getDataForNearestPositionGBuser(@PathVariable int id) {
		logger.info("test");
		logger.debug("testdebug");
		List<PositionHistory> resultlist=(List<PositionHistory>) dph.findByNearestPositionGBuser(id, 0, 10, null, null, null);
		return resultlist;
	}
}
